using JT808Protocol.Common;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.CommonResponse
{
    /// <summary>
    /// 平台通用应答
    /// </summary>
    public class DpPlatform_0x8001 : IJT808Body
    {
        /// <summary>
        /// 应答流水号
        /// </summary>
        [JsonPropertyName("应答流水号")]
        public ushort SerialNum { get; set; }

        /// <summary>
        /// 应答ID
        /// </summary>
        [JsonPropertyName("应答ID")]
        public ushort ID { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        [JsonPropertyName("结果")]
        public EnumResult Result { get; set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> data, ref int index)
        {
            SerialNum = ParseHelper.Uint16(ref data, index);
            index += 2;
            ID = ParseHelper.Uint16(ref data, index);
            index += 2;
            Result = (EnumResult)Convert.ToInt32(data[index]);

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            List<byte> result = new List<byte>();
            //应答流水号
            result.AddRange(ParseHelper.Bytes(SerialNum));

            //应答ID
            result.AddRange(ParseHelper.Bytes(ID));

            //结果
            result.Add((byte)Result);

            return result;
        }
    }
}